package com.ossfk.jt808.client;

import com.ossfk.jt808.client.adapter.Jt808ClientAdapter;
import com.ossfk.jt808.codec.Jt808Decoder;
import com.ossfk.jt808.codec.Jt808Encoder;
import com.ossfk.jt808.pojo.Jt808Msg;
import com.ossfk.jt808.utils.Jt808Utils;
import com.ossfk.net.client.AbstractTcpClient;
import com.ossfk.net.client.adapter.AbstractClientAdapter;
import com.ossfk.net.config.NetClientConfig;

import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * JT808客户端，遵循交通部808协议通讯
 * @ClassName: Jt808Client
 * @author Liujiefu
 * @date 2020-03-17 13:49:25
 */
public class Jt808Client extends AbstractTcpClient {

	public Jt808Client() {
		super();
	}

	public Jt808Client(NetClientConfig clientConfig) {
		super(clientConfig);
	}

	@Override
	public void sendMsg(Object msg) {
		int flowId = 0;
		if (getSession() != null)
			flowId = getSession().nextFlowId();
		if (msg instanceof Jt808Msg) {
			Jt808Msg jt808Msg = (Jt808Msg) msg;
			// 补上流水号
			jt808Msg.getHeader().setFlowId(flowId);
		}
		super.sendMsg(msg);
	}

	@Override
	protected DelimiterBasedFrameDecoder getFrameDecoder() {
		return Jt808Utils.getFrameDecoder();
	}

	@Override
	protected ByteToMessageDecoder getMsgDecoder() {
		return new Jt808Decoder();
	}

	@Override
	protected MessageToByteEncoder<?> getMsgEncoder() {
		return new Jt808Encoder();
	}

	@Override
	protected AbstractClientAdapter getChannelAdapter() {
		return new Jt808ClientAdapter();
	}

}
